﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.WorkFlow.DataAccess;
using VitCode.WorkFlow.Entities;

namespace WorkFlowSettingApp
{
    public partial class SelObject : Form
    {
        public string ListSql { get; set; }

        public string KeyProName { get; set; }

        public string ClassName { get; set; }

        public object ObjectVal { get; set; }

        public WorkItem WorkItem { get; set; }
        
        public SelObject()
        {
            InitializeComponent();
        }

        public string QueryCondition { get; set; }

        private void SetControls()
        {
            SqlHelper sqlHelper = new SqlHelper();
            WorkFlowAccess acc = new WorkFlowAccess();
            try
            {
                DataTable dt = sqlHelper.ExecuteDataSet(acc.Session, ListSql+" Where 1=2 ").Tables[0];
                if (dt != null)
                {
                    dgvList.DataSource = dt;
                    foreach(DataGridViewColumn col in dgvList.Columns)
                    {
                        if(col.Name == KeyProName)
                        {
                            col.Visible = false;
                            continue;
                        }
                        cboCondition.Items.Add(col.Name);
                    }
                }
            }
            finally
            {
                acc.CloseSession();
            }
            if (cboCondition.Items.Count > 0)
                cboCondition.SelectedIndex = 0;
        }

        private void dgvList_RowDividerDoubleClick(object sender, DataGridViewRowDividerDoubleClickEventArgs e)
        {
            
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            
            if (txtVal.Text.Length > 0)
                QueryCondition = " Where "+cboCondition.SelectedItem+" like '%"+txtVal.Text+"%' ";
            string sql = ListSql + QueryCondition;
            SqlHelper sqlHelper = new SqlHelper();
            WorkFlowAccess acc = new WorkFlowAccess();
            try
            {
                DataTable dt = sqlHelper.ExecuteDataSet(acc.Session, sql).Tables[0];
                dgvList.DataSource = dt;
            }
            finally
            {
                acc.CloseSession();
            }
        }

        private void SelObject_Load(object sender, EventArgs e)
        {
            SetControls();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                DataGridViewRow dr = dgvList.Rows[e.RowIndex];
                string strKeyProVal = (dr.Cells[KeyProName].Value.ToString());
                BaseDataAccess acc = new BaseDataAccess();
                
                try
                {
                    var obj = acc.GetSingleObjectByProVal(ClassName, KeyProName, strKeyProVal);
                    if (obj != null && obj is Role)
                    {
                        WorkItem.OperateRole = ((Role)obj);
                        acc.SaveOrUpdate(WorkItem);
                    }
                }
                finally
                {
                    acc.CloseSession();
                }
            }
            Close();
        }
    }
}
